var Config = require('../../lib/config');
var Uploader = require('../../lib/UploaderV5.js');
var app = getApp();
var c = require('../../lib/common.js');
var upimg = require('../../lib/upimg.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vhidden: true,
    topname: '意见反馈',
    prepage: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    //获得session值
    wx.getStorage({
      key: '3rd_seession',
      success: function(res) {
        that.setData({
          rd_seession: res.data.data.rd_session
        });
      }
    })


    this.recorder = wx.getRecorderManager();
    //音频录制完成后执行上传操作
    this.recorder.onStart((res) => {
      console.debug('start record')
    });
    this.recorder.onStop((res) => {
      console.debug('end record')
      this.uploadFile(res.tempFilePath);
    });
    this.setData({
      hiddeninfo: true
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  simpleUpload: function(e) {
    var path = 'chongwulingyang/img/' + new Date().getTime() + '.png';
    var that = this;
    var returninfo = upimg.simpleUpload(
      path,
      function(res) {
        setTimeout(function() {
          that.setData({
            arrayimg: res
          })
        }, 1500)
      },
      6
    );
  },
  //页面事件绑定，长按按钮的开始
  record_start: function() {
    var timedata = new Date();
    var timestring = timedata.getTime();
    this.setData({
      stime: timestring,
      hiddeninfo: false
    })
    this.recorder.start({
      format: "mp3"
    });
  },

  //页面事件绑定，长按按钮的结束
  record_end: function() {
    var longclick = this.data.longclick;
    if (longclick != 1) {
      wx.showToast({
        title: '录音时间短！',
        icon: 'none'
      })
      this.setData({
        hiddeninfo: true,

      });
      return;
    }
    var timedata = new Date();
    var timestring = timedata.getTime();
    var time = timestring - this.data.stime;
    var sint = parseInt(time / 1000);
    var mint = parseInt((time - sint * 1000) / 10);
    var long = sint * 20 + 'rpx';
    this.setData({
      longclick: 0,
      sint: sint,
      mint: mint,
      long: long,
      hiddeninfo: true,
      vhidden: false,
      longtime: time
    });
    this.recorder.stop();
  },
  //长按
  simpleRecorder: function() {
    this.setData({
      longclick: 1
    });
  },
  button: function() {

  },
  // 简单上传文件
  uploadFile(tempFilePath) {
    console.error('uploadFile', tempFilePath);
    var ntime = new Date();
    var time = ntime.getTime();
    var pathurl = '/chongwulingyang/send/' + time + 'demand';
    var that = this;
    Uploader.upload(tempFilePath, function(result) {

      if (Config.COSv4) {
        var audioUrl = result.data.data.source_url.replace("http://", "https://")
      } else {
        var audioUrl = result.Location.replace("http://", "https://")
      }

      var audioUrlnew = audioUrl.replace('uploadFile', '')
      //更新demo的数据展示
      that.setData({
        sendAudioList: audioUrlnew,
        imgurl: pathurl + '.mp3'
      })
    }, '', pathurl);
  },

  avdiov: function() {
    const innerAudioContext = wx.createInnerAudioContext()
    var path = this.data.sendAudioList;
    innerAudioContext.autoplay = true;
    innerAudioContext.src = path;
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },
  bindinputclick: function(e) {
    var inputvalue_time = e.detail.value;
    this.setData({
      inputvalue_time: inputvalue_time
    });
  },
  bindinputTextarea: function(e) {
    var inputvalue_textarea = e.detail.value;
    this.setData({
      inputvalue_textarea: inputvalue_textarea
    });
  },
  submit: function() {
    //uid
    var uid=wx.getStorageSync('userinfo').id;
    //描述
    var inputvalue_textarea = this.data.inputvalue_textarea;
    //图片数组 url
    var arrayimg = this.data.arrayimg;
    //语音 url
    var sendAudioList = this.data.imgurl;
    //语音时长
    var longtime = Math.round(this.data.longtime / 1000);
    var arrayimgnew = '';
    if (arrayimg) {
      for (var i = 0; i < arrayimg.length; i++) {
        arrayimgnew += arrayimg[i]['dbs'] + ',';
      }
      arrayimgnew = arrayimgnew.substring(0, arrayimgnew.length - 1);
    }
    if (typeof(sendAudioList) == 'undefined') {
      sendAudioList = '';
      longtime = 0;
    }
    wx.request({
      url: 'https://www.cwlyz.com/wx/Index/toOrderIdChangOrderInfo', //仅为示例，并非真实的接口地址
      data: {
        uid: uid,
        arrayimg: arrayimgnew,
        sendAudioList: sendAudioList,
        longtime: longtime,
        inputvalue_textarea: inputvalue_textarea
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        c.pan('感谢您的反馈！');
        setTimeout(function () {
          if (res.data) {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }},2000)
      }
    })

  }
})